package com.pinyougou.user.service;

import com.pinyougou.pojo.TbUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;

public class UserDetailServiceImpl implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //授权认证集合
        ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<>();

        //添加认证类
        grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER"));

        //返回一个security封装的用户对象，添加认证身份集合
        return new User(username,"",grantedAuthorities);
    }

    public TbUser getCurrentLoginUser() {
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        TbUser tbUser = new TbUser();

        tbUser.setUsername(name);

        return tbUser;
    }
    
}
